/*Joseph A. Coll and Taylor Tokos
1/6/2025
Racial Polarization and Voter Confidence Sankey Plots*/

ssc install sankeyplot

*Full Sample
use "2016 2020 ANES Combined.dta", clear

keep if white_non_hispanic == 1

tab vote_conf_2016 if vote_conf_2020  != .
tab vote_conf_2020 if vote_conf_2016  != .

keep V160001_orig panel_year vote_conf white_non_hispanic

reshape wide vote_conf, i(V160001_orig) j(panel_year)

collapse (count) n = V160001_orig, by(vote_conf1 vote_conf2)

expand n
drop n

graph set window fontface "Times New Roman"

sankeyplot vote_conf1 vote_conf2, ///
    xlabel(0 "2016" 1 "2020", nogrid) ///
	title("", ///
       margin(b=0) size(med) position(12)) ///
    xtitle("Election Year") ///
	legend(size(vsmall) symysize(vsmall) symxsize(medsmall)) ///
	ylabel(, nogrid noticks) ///
	yscale(off)  ///
	text(22 0 "2.54", size(vsmall) color(black)) ///
	text(100 0 "6.96", size(vsmall) color(black)) ///
	text(250 0 "9.45", size(vsmall) color(black)) ///
	text(900 0 "61.32", size(vsmall) color(black)) ///
	text(1700 0 "19.73", size(vsmall) color(black)) ///
	text(42 1 "5.40", size(vsmall) color(black)) ///
	text(225 1 "13.71", size(vsmall) color(black)) ///
	text(440 1 "10.49", size(vsmall) color(black)) ///
	text(950 1 "43.72", size(vsmall) color(black)) ///
	text(1600 1 "26.69", size(vsmall) color(black))  ///
	name(sankey_graph_full, replace)
graph export "sankey_graph.png", as(png)  replace

*High RPA:WB
use "2016 2020 ANES Combined.dta", clear

ssc install sankeyplot
sum rap_wb
keep if white_non_hispanic == 1
drop if rap_wb < 0

tab vote_conf_2016 if vote_conf_2020  != .
tab vote_conf_2020 if vote_conf_2016  != .

keep V160001_orig panel_year vote_conf white_non_hispanic

reshape wide vote_conf, i(V160001_orig) j(panel_year)

collapse (count) n = V160001_orig, by(vote_conf1 vote_conf2)

expand n
drop n

graph set window fontface "Times New Roman"

sankeyplot vote_conf1 vote_conf2, ///
    xlabel(0 "2016" 1 "2020", nogrid) ///
	title("", ///
       margin(b=0) size(med) position(12)) ///
    xtitle("Election Year") ///
	legend(size(vsmall) symysize(vsmall) symxsize(medsmall)) ///
	ylabel(, nogrid noticks) ///
	yscale(off)  ///
	text(12 0 "1.99", size(vsmall) color(black)) ///
	text(75 0 "6.67", size(vsmall) color(black)) ///
	text(165 0 "9.91", size(vsmall) color(black)) ///
	text(700 0 "62.43", size(vsmall) color(black)) ///
	text(1200 0 "19.00", size(vsmall) color(black)) ///
	text(42 1 "5.92", size(vsmall) color(black)) ///
	text(205 1 "15.14", size(vsmall) color(black)) ///
	text(400 1 "11.96", size(vsmall) color(black)) ///
	text(775 1 "42.80", size(vsmall) color(black)) ///
	text(1250 1 "24.17", size(vsmall) color(black))  ///
	name(sankey_graph_rap_wb_hi, replace)
graph export "sankey_graph_rap_wb_high.png", as(png)  replace

*Low RPA:WB
use "2016 2020 ANES Combined.dta", clear

ssc install sankeyplot
sum rap_wb
keep if white_non_hispanic == 1
drop if rap_wb > 0

tab vote_conf_2016 if vote_conf_2020  != .
tab vote_conf_2020 if vote_conf_2016  != .

keep V160001_orig panel_year vote_conf white_non_hispanic

reshape wide vote_conf, i(V160001_orig) j(panel_year)

collapse (count) n = V160001_orig, by(vote_conf1 vote_conf2)

expand n
drop n

graph set window fontface "Times New Roman"

sankeyplot vote_conf1 vote_conf2, ///
    xlabel(0 "2016" 1 "2020", nogrid) ///
	title("", ///
       margin(b=0) size(med) position(12)) ///
    xtitle("Election Year") ///
	legend(size(vsmall) symysize(vsmall) symxsize(medsmall)) ///
	ylabel(, nogrid noticks) ///
	yscale(off)  ///
	text(15 0 "3.10", size(vsmall) color(black)) ///
	text(70 0 "7.50", size(vsmall) color(black)) ///
	text(150 0 "8.74", size(vsmall) color(black)) ///
	text(500 0 "60.91", size(vsmall) color(black)) ///
	text(900 0 "19.75", size(vsmall) color(black)) ///
	text(17 1 "4.68", size(vsmall) color(black)) ///
	text(87 1 "12.94", size(vsmall) color(black)) ///
	text(200 1 "9.57", size(vsmall) color(black)) ///
	text(450 1 "44.60", size(vsmall) color(black)) ///
	text(825 1 "28.22", size(vsmall) color(black))  ///
	name(sankey_graph_rap_wb_low, replace)
	
graph export "sankey_graph_rap_wb_low.png", as(png)  replace


*High RPA:WL
use "2016 2020 ANES Combined.dta", clear

ssc install sankeyplot
sum rap_wb
keep if white_non_hispanic == 1
drop if rap_wl < 0

tab vote_conf_2016 if vote_conf_2020  != .
tab vote_conf_2020 if vote_conf_2016  != .

keep V160001_orig panel_year vote_conf white_non_hispanic

reshape wide vote_conf, i(V160001_orig) j(panel_year)

collapse (count) n = V160001_orig, by(vote_conf1 vote_conf2)

expand n
drop n

graph set window fontface "Times New Roman"

sankeyplot vote_conf1 vote_conf2, ///
    xlabel(0 "2016" 1 "2020", nogrid) ///
	title("", ///
       margin(b=0) size(med) position(12)) ///
    xtitle("Election Year") ///
	legend(size(vsmall) symysize(vsmall) symxsize(medsmall)) ///
	ylabel(, nogrid noticks) ///
	yscale(off)  ///
	text(12 0 "2.17", size(vsmall) color(black)) ///
	text(75 0 "6.45", size(vsmall) color(black)) ///
	text(165 0 "9.96", size(vsmall) color(black)) ///
	text(700 0 "62.26", size(vsmall) color(black)) ///
	text(1200 0 "19.16", size(vsmall) color(black)) ///
	text(42 1 "6.00", size(vsmall) color(black)) ///
	text(205 1 "15.07", size(vsmall) color(black)) ///
	text(400 1 "11.94", size(vsmall) color(black)) ///
	text(775 1 "43.17", size(vsmall) color(black)) ///
	text(1250 1 "23.82", size(vsmall) color(black))  ///
	name(sankey_graph_rap_wl_hi, replace)
graph export "sankey_graph_rap_wl_high.png", as(png)  replace

*Low RPA:WL
use "2016 2020 ANES Combined.dta", clear

ssc install sankeyplot
sum rap_wb
keep if white_non_hispanic == 1
drop if rap_wl > 0

tab vote_conf_2016 if vote_conf_2020  != .
tab vote_conf_2020 if vote_conf_2016  != .

keep V160001_orig panel_year vote_conf white_non_hispanic

reshape wide vote_conf, i(V160001_orig) j(panel_year)

collapse (count) n = V160001_orig, by(vote_conf1 vote_conf2)

expand n
drop n

graph set window fontface "Times New Roman"

sankeyplot vote_conf1 vote_conf2, ///
    xlabel(0 "2016" 1 "2020", nogrid) ///
	title("", ///
       margin(b=0) size(med) position(12)) ///
    xtitle("Election Year") ///
	legend(size(vsmall) symysize(vsmall) symxsize(medsmall)) ///
	ylabel(, nogrid noticks) ///
	yscale(off)  ///
	text(14 0 "2.93", size(vsmall) color(black)) ///
	text(67 0 "7.08", size(vsmall) color(black)) ///
	text(130 0 "8.92", size(vsmall) color(black)) ///
	text(475 0 "60.86", size(vsmall) color(black)) ///
	text(900 0 "20.22", size(vsmall) color(black)) ///
	text(20 1 "4.83", size(vsmall) color(black)) ///
	text(100 1 "13.00", size(vsmall) color(black)) ///
	text(200 1 "9.53", size(vsmall) color(black)) ///
	text(500 1 "44.04", size(vsmall) color(black)) ///
	text(900 1 "28.59", size(vsmall) color(black))  ///
	name(sankey_graph_rap_wl_low, replace)
graph export "sankey_graph_rap_wl_low.png", as(png)  replace

*High RPA:II
use "2016 2020 ANES Combined.dta", clear

ssc install sankeyplot
sum rap_wb
keep if white_non_hispanic == 1
drop if rap_wii < 0

tab vote_conf_2016 if vote_conf_2020  != .
tab vote_conf_2020 if vote_conf_2016  != .

keep V160001_orig panel_year vote_conf white_non_hispanic

reshape wide vote_conf, i(V160001_orig) j(panel_year)

collapse (count) n = V160001_orig, by(vote_conf1 vote_conf2)

expand n
drop n

graph set window fontface "Times New Roman"

sankeyplot vote_conf1 vote_conf2, ///
    xlabel(0 "2016" 1 "2020", nogrid) ///
	title("", ///
       margin(b=0) size(med) position(12)) ///
    xtitle("Election Year") ///
	legend(size(vsmall) symysize(vsmall) symxsize(medsmall)) ///
	ylabel(, nogrid noticks) ///
	yscale(off)  ///
	text(15 0 "2.31", size(vsmall) color(black)) ///
	text(78 0 "6.64", size(vsmall) color(black)) ///
	text(175 0 "9.87", size(vsmall) color(black)) ///
	text(800 0 "62.09", size(vsmall) color(black)) ///
	text(1400 0 "19.10", size(vsmall) color(black)) ///
	text(42 1 "5.94", size(vsmall) color(black)) ///
	text(205 1 "14.77", size(vsmall) color(black)) ///
	text(425 1 "11.25", size(vsmall) color(black)) ///
	text(850 1 "43.74", size(vsmall) color(black)) ///
	text(1400 1 "24.29", size(vsmall) color(black))  ///
	name(sankey_graph_rap_wii_hi, replace)
graph export "sankey_graph_rap_wii_high.png", as(png)  replace

*Low RPA:II
use "2016 2020 ANES Combined.dta", clear

ssc install sankeyplot
sum rap_wb
keep if white_non_hispanic == 1
drop if rap_wii > 0

tab vote_conf_2016 if vote_conf_2020  != .
tab vote_conf_2020 if vote_conf_2016  != .

keep V160001_orig panel_year vote_conf white_non_hispanic

reshape wide vote_conf, i(V160001_orig) j(panel_year)

collapse (count) n = V160001_orig, by(vote_conf1 vote_conf2)

expand n
drop n

graph set window fontface "Times New Roman"

sankeyplot vote_conf1 vote_conf2, ///
    xlabel(0 "2016" 1 "2020", nogrid) ///
	title("", ///
       margin(b=0) size(med) position(12)) ///
    xtitle("Election Year") ///
	legend(size(vsmall) symysize(vsmall) symxsize(medsmall)) ///
	ylabel(, nogrid noticks) ///
	yscale(off)  ///
	text(5 0 "4.61", size(vsmall) color(black)) ///
	text(20 0 "8.33", size(vsmall) color(black)) ///
	text(40 0 "9.75", size(vsmall) color(black)) ///
	text(100 0 "57.09", size(vsmall) color(black)) ///
	text(190 0 "20.21", size(vsmall) color(black)) ///
	text(3 1 "3.72", size(vsmall) color(black)) ///
	text(12 1 "7.45", size(vsmall) color(black)) ///
	text(25 1 "8.16", size(vsmall) color(black)) ///
	text(75 1 "41.31", size(vsmall) color(black)) ///
	text(160 1 "39.36", size(vsmall) color(black))  ///
	name(sankey_graph_rap_wii_low, replace)
graph export "sankey_graph_rap_wii_low.png", as(png)  replace
